package com.hz.friends.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

import com.androidquery.AQuery;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.hz.friends.application.CustomApplication;
import com.hz.friends.serverrequest.ALUserEngine;
import com.hz.friends.serverrequest.ALEngineConfig.CallBack;
import com.hz.friends.utils.MLog;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TheGiftActivity extends BaseActivity {
	private PullToRefreshListView mlistview;
	private LinearLayout layout_tips;
	private FrameLayout layout_levelmsg;
	private LinearLayout includebar;
	private AVUser user = null;
	private final int limit = 20;// 分页
	private int skip = 0;// 跳过记录条数
	private AQuery aq;
	private MyAdpter adpter;
	private final String TAG = "TheGiftActivity";
	private TextView txv_nodata;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_thegift);
		init();
		initViews();
		uploadGiftList();
	}

	@Override
	protected void init() {
		user = ((CustomApplication) getApplication()).user;
		aq = new AQuery(this);

	}

	@Override
	protected void initViews() {
		layout_levelmsg = (FrameLayout) findViewById(R.id.layout_levelmsg);
		layout_tips = (LinearLayout) findViewById(R.id.layout_tips);
		includebar = (LinearLayout) findViewById(R.id.includebar);

		ImageButton imbtn_bar_left = (ImageButton) findViewById(R.id.imbtn_bar_left);
		ImageButton imbtn_bar_right = (ImageButton) findViewById(R.id.imbtn_bar_right);
		TextView imbtn_bar_title = (TextView) findViewById(R.id.imbtn_bar_title);
		imbtn_bar_right.setVisibility(View.INVISIBLE);
		imbtn_bar_left.setImageResource(R.drawable.ic_leftback_arrow);
		imbtn_bar_title.setText("Gift");
		imbtn_bar_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		ImageButton imbtn_botomback = (ImageButton) findViewById(R.id.imbtn_botomback);
		imbtn_botomback.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
        txv_nodata = (TextView)findViewById(R.id.txv_nodata);

		Button btn_message = (Button) findViewById(R.id.btn_message);
		Button btn_present = (Button) findViewById(R.id.btn_present);
		btn_message.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(LevelMessageActivity.class);
			}
		});

		btn_present.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(SendGiftActivity.class);
			}
		});

		mlistview = (PullToRefreshListView) findViewById(R.id.mlistview);

		adpter = new MyAdpter(this,new ArrayList<HashMap>());
		mlistview.setAdapter(adpter);

		int pageflag = getIntent().getIntExtra("pageflag", 0);
		if (pageflag == 1) {

		} else {
			layout_levelmsg.setVisibility(View.GONE);
			layout_tips.setVisibility(View.GONE);
			includebar.setVisibility(View.VISIBLE);
		}

		AVUser curruser = AVUser.getCurrentUser();
		if (curruser.getObjectId().equals(user.getObjectId())) {// 自己
			btn_message.setVisibility(View.GONE);
			btn_present.setVisibility(View.GONE);
		}
	}

	@Override
	protected void setViews() {

	}

	@Override
	protected void initEvents() {
		mlistview.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				adpter.clearData();
				uploadGiftList();
			}

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
//				adpter.clearData();
//				skip = 0;
//				uploadGiftList();
			}
		});
	}
	
	private void uploadGiftList() {
		ALUserEngine.defauleEngine().getReceivedGiftCount(user, new CallBack() {

			@Override
			public void done(Object object, AVException avexception) {
				MLog.i(TAG, "getReceivedGiftCount avexception:" + avexception
						+ "  object:" + object);
				if (avexception == null) {
					ArrayList<HashMap> maplist = (ArrayList<HashMap>) object;
					if (maplist == null || maplist.size() == 0)
						return;
			       adpter.addData(maplist);
			       adpter.notifyDataSetChanged();
				} else {
					avexception.printStackTrace();
				}
			}
		});
	}


	class MyAdpter extends BaseAdapter {

		ArrayList<HashMap> maplist = null;
		Context context = null;

		@Override
		public int getCount() {
			return maplist.size();
		}

		public MyAdpter(Context context, ArrayList<HashMap> maplist) {
                 this.context = context;
                 this.maplist = maplist;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		public void addData( ArrayList<HashMap> maplist){
			this.maplist.addAll(maplist);
		}
		
		public void clearData(){
			this.maplist.clear();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = LayoutInflater.from(TheGiftActivity.this).inflate(
					R.layout.adpter_thegift, null);
			// ImageView imv_heard =
			// (ImageView)view.findViewById(R.id.imv_heard);
			TextView txv_name = (TextView) view.findViewById(R.id.txv_name);
			LinearLayout layout_sex = (LinearLayout) view
					.findViewById(R.id.layout_sex);
			ImageView imv_gft1 = (ImageView) view.findViewById(R.id.imv_gft1);
			ImageView imv_sex = (ImageView) view.findViewById(R.id.imv_sex);
			TextView txv_age = (TextView) view.findViewById(R.id.txv_age);
			TextView txv_clock = (TextView) view.findViewById(R.id.txv_clock);
			TextView txv_giftnum = (TextView) view.findViewById(R.id.txv_giftnum);
			TextView txv_content = (TextView) view
					.findViewById(R.id.txv_content);

			HashMap<String, Object> mapitem = maplist.get(position);
			Date create = (Date) mapitem.get("updatedAt");
			AVObject giftobj = (AVObject) mapitem.get("gift");
			String gifturl = giftobj.getString("imageURL");
			int num = (Integer) mapitem.get("number");
			txv_giftnum.setText(String.valueOf(num));
			SimpleDateFormat spf = new SimpleDateFormat("MM-dd HH:mm");
			txv_clock.setText(spf.format(create));
			
			aq.id(imv_gft1).image(gifturl, true, true);
			
			return view;
		}

	}
}
